<?php


namespace App\Shuixing\Controllers;


use App\Dengje\Utils\Settings;
use App\Http\Controllers\Api\BaseController;
use App\Lib\JwtAuth;
use App\Models\Posts\WxLike;
use App\Models\WxCommentLike;
use App\Models\WxUser;
use App\Models\WxUserFollow;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Storage;
use JiaweiXS\WeApp\WeApp;

class LoginController extends BaseController
{
    public function index()
    {

        //例如，在laravel,文件缓存路径为'../storage/cache/'
        $weapp = new WeApp(Settings::get('app_id'), Settings::get('app_secret'), '../storage/cache/');
        //从‘小程序’获取一个‘二维码’单例对象
        $qrcode = $weapp->getQRCode();
        $scene = md5(uniqid());
//        $page = 'pages/login/login';
        $page = 'pages/pc-login/index';
        //2.获取小程序B码
        $qrcodeContent = $qrcode->getQRCodeB($scene, $page, 430, false, $line_color = null);
        $filePath = 'qrcode/' . md5(uniqid()) . '.png';
        $res = Storage::disk('public')->put($filePath, $qrcodeContent);
        if ($res) {
            $qrcodeurl = Storage::url($filePath);
        }
        return view('shuixing/login', compact('qrcodeurl', 'scene'));
    }

    public function pcUserinfo(Request $request)
    {
        $token = $request->header('token');
        $uid = 0;
        if ($token) {
            $uid = JwtAuth::getUserId($token, 'uid');
            Cache::put('isLogin', true, $seconds = 43200);
        }
        $data = WxUser::where('id', $uid)
            ->first(['user_name', 'user_avatar', 'user_introduce', 'user_background_maps', 'is_authentication', 'is_member', 'is_official'])
            ->toArray();

        $data['followTotal'] = WxUserFollow::where('user_id', $uid)->count();
        $data['fansTotal'] = WxUserFollow::where('user_follow_id', $uid)->count();
        $count1 = WxLike::where('posts_user_id', $uid)->count();
        $count2 = WxCommentLike::where('comment_user_id', $uid)->count();
        $data['likeTotal'] = $count1 + $count2;
        $data['token'] = $token;

        return $data;
    }
}
